﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Fmis21_10.Models;

namespace Fmis21_10.Controllers
{
    public class MatchController : Controller
    {
        private FmisEntities db = new FmisEntities();

        //
        // GET: /Match/

        public ActionResult Index()
        {
            var matches = db.Matches.Include(m => m.Stadium).Include(m => m.Tournament).OrderBy(m=>m.TournamentId);
            return View(matches.ToList());
        }

        //
        // GET: /Match/Details/5

        public ActionResult Details(int id = 0)
        {
            Match match = db.Matches.Find(id);
            if (match == null)
            {
                return HttpNotFound();
            }
            return View(match);
        }

        //
        // GET: /Match/Create

        public ActionResult Create(int id=0)
        {
            //get dropdownlist for Team
            string Chuoi = "";
            var selectTeam = (from t in db.Teams join tt in db.TournamentTeams on t.TeamId equals tt.TeamId where tt.TournamentId==id select t).Take(200).ToList();
                               
            
            for (int i = 0; i < selectTeam.Count; i++)
            { 
                Chuoi+="<option value=" + selectTeam[i].TeamId+ ">"+selectTeam[i].TeamName +"</option>";
                 
            }
            ViewBag.selectTeam = Chuoi;
            Tournament tour = db.Tournaments.Find(id);
            ViewBag.StadiumId = new SelectList(db.Stadiums, "StadiumId", "StadiumName");
            ViewBag.TournamentId = new SelectList(db.Tournaments, "TournamentId", "TournamentName");
            ViewBag.TeamId = new SelectList(db.Teams, "TeamId", "TeamName");
            //var selectTeam = new SelectList(db.Teams, "TeamId","TeamName");
            //ViewBag.TeamId = new SelectList(db.Teams, "TeamId", "TeamName");
            if (tour == null)
            {
                
            }
            else {
                ViewBag.TournamentId = new SelectList(db.Tournaments, "TournamentId", "TournamentName", tour.TournamentId);
            }
            return View();
        }
         
        //
        // POST: /Match/Create

        [HttpPost]
        public ActionResult Create(Match match, FormCollection collection, MatchInformation matchinfo1, MatchInformation matchinfo2)
        {


            if (ModelState.IsValid)
            {
                int iHomeTeam = int.Parse(collection["HomeTeam"]);
                var HomeTeam = (from ht in db.Teams where ht.TeamId == iHomeTeam select ht).Take(1).ToList();
                match.HomeTeam = HomeTeam[0].TeamName;
                int iAwayTeam = int.Parse(collection["AwayTeam"]);
                var AwayTeam = (from ht in db.Teams where ht.TeamId == iAwayTeam select ht).Take(1).ToList();
                match.AwayTeam = AwayTeam[0].TeamName;
                db.Matches.Add(match);
                db.SaveChanges();
                if(match.ScoreHomeTeam>match.ScoreAwayTeam){
                    matchinfo1.Result = 3;
                    matchinfo2.Result = 0;
                }
                else if (match.ScoreHomeTeam == match.ScoreAwayTeam)
                {
                    matchinfo1.Result = matchinfo2.Result = 1;
                }
                else{
                    matchinfo1.Result = 0;
                    matchinfo2.Result = 3;
                }
                matchinfo1.TeamId = iHomeTeam;
                var matchIdmatchinfo = (from m in db.Matches orderby m.MatchId descending select m).Take(1).ToList();
                matchinfo1.MatchId = int.Parse(matchIdmatchinfo[0].MatchId.ToString());//matchIdmatchinfo[0].MatchId;
                matchinfo1.Score = match.ScoreHomeTeam;
                db.MatchInformations.Add(matchinfo1);
                db.SaveChanges();
                matchinfo2.TeamId = iAwayTeam;
                matchinfo2.MatchId = matchinfo1.MatchId;
                matchinfo2.Score = match.ScoreAwayTeam;
                db.MatchInformations.Add(matchinfo2);
                db.SaveChanges();

                return RedirectToAction("Index");
            }
            ViewBag.StadiumId = new SelectList(db.Stadiums, "StadiumId", "StadiumName", match.StadiumId);
            ViewBag.TournamentId = new SelectList(db.Tournaments, "TournamentsId", "TournamentsName", match.TournamentId);
            ViewBag.TeamId1 = new SelectList(db.Teams, "TeamId", "TeamName", match.HomeTeam);
            
            return View(match);
        }

        //
        // GET: /Match/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Match match = db.Matches.Find(id);
            if (match == null)
            {
                return HttpNotFound();
            }
            ViewBag.StadiumId = new SelectList(db.Stadiums, "StadiumId", "StadiumName", match.StadiumId);
            ViewBag.TournamentId = new SelectList(db.Tournaments, "TournamentsId", "TournamentsName", match.TournamentId);
            return View(match);
        }
        [HttpPost]
        public ActionResult Edit(Match match, MatchInformation matchinfo1, MatchInformation matchinfo2, Rating rat)
        {

            if (ModelState.IsValid)
            {

                db.Entry(match).State = EntityState.Modified;
                db.SaveChanges();
                var getIdTeam = (from t in db.Teams where t.TeamName == match.HomeTeam select t).Take(1).ToList();
                int HomeTeamId = getIdTeam[0].TeamId;
                var getIdMatchInfo1 = (from m in db.MatchInformations where (m.MatchId == match.MatchId && m.TeamId == HomeTeamId) select m).Take(1).ToList();
                int MatchInfoId1 = getIdMatchInfo1[0].MatchInformationId;
                matchinfo1.Score = match.ScoreHomeTeam;
                UpdateModel(matchinfo1);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.StadiumId = new SelectList(db.Stadiums, "StadiumId", "StadiumName", match.StadiumId);
            ViewBag.TournamentId = new SelectList(db.Tournaments, "TournamentsId", "TournamentsName", match.TournamentId);
            return View(match);
        }
        [HttpPost]
        public ActionResult UpdateScore(FormCollection fc, int id = 0)
        {
            var match = db.Matches.First(m => m.MatchId == id);
            int iScoreHomeTeam = int.Parse(fc["ScoreHomeTeam"]);
            int iScoreAwayTeam = int.Parse(fc["ScoreAwayTeam"]);
            //get HomeTeamId
            var getHomeTeamId = (from t in db.Teams where t.TeamName == match.HomeTeam select t).Take(1).ToList();
            int HomeTeamId = getHomeTeamId[0].TeamId;
            var getRatingId1 = (from r in db.Ratings where (r.TeamId == HomeTeamId && r.TournamentId == match.TournamentId) select r).Take(1).ToList();
            int RatingHomeTeamId = getRatingId1[0].RatingId;
            var ratingHomeTeam = db.Ratings.First(r => r.RatingId == RatingHomeTeamId);
            //get AwayTeamId
            var getAwayTeamId = (from t in db.Teams where t.TeamName == match.AwayTeam select t).Take(1).ToList();
            int AwayTeamId = getAwayTeamId[0].TeamId;
            var getRatingId2 = (from r in db.Ratings where (r.TeamId == AwayTeamId && r.TournamentId == match.TournamentId) select r).Take(1).ToList();
            int RatingAwayTeamId = getRatingId2[0].RatingId;
            var ratingAwayTeam = db.Ratings.First(r => r.RatingId == RatingAwayTeamId);
            if (iScoreHomeTeam > iScoreAwayTeam)
            {
                ratingHomeTeam.Point = ratingHomeTeam.Point + 3;
                ratingHomeTeam.Win = ratingHomeTeam.Win + 1;
                ratingAwayTeam.Lose = ratingAwayTeam.Lose + 1;
            }
            else if (iScoreHomeTeam == iScoreAwayTeam)
            {
                ratingHomeTeam.Point = ratingHomeTeam.Point + 1;
                ratingAwayTeam.Point = ratingAwayTeam.Point + 1;
                ratingHomeTeam.Draw = ratingHomeTeam.Draw + 1;
                ratingAwayTeam.Draw = ratingAwayTeam.Draw + 1;
            }
            else
            {
                ratingAwayTeam.Point = ratingAwayTeam.Point + 3;
                ratingAwayTeam.Win = ratingAwayTeam.Win + 1;
                ratingHomeTeam.Lose = ratingHomeTeam.Lose + 1;
            }
            UpdateModel(ratingHomeTeam);
            UpdateModel(ratingAwayTeam);
            match.ScoreHomeTeam = iScoreHomeTeam;
            match.ScoreAwayTeam = iScoreAwayTeam;
            UpdateModel(match);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        public ActionResult UpdateScore(int id = 0)
        {
            var match = db.Matches.First(m => m.MatchId == id);
            return View(match);
        }
        public ActionResult UpdateScorehihi(int id = 0)
        {
            var match = db.Matches.First(m => m.MatchId == id);
            return PartialView(match);
        }
        public ActionResult UpdateScorehaha(int id = 0)
        {
            var match = db.Matches.Find(id);
            return PartialView(match);
        }
        public ActionResult UpdateScoretenten(int id = 0)
        {
            var match = db.Matches.Find(id);
            return PartialView(match);
        }
        [HttpPost]
        public ActionResult UpdateScoretenten(FormCollection fc, int id = 0)
        {
            var match = db.Matches.First(m => m.MatchId == id);
            int iScoreHomeTeam = int.Parse(fc["ScoreHomeTeam"]);
            int iScoreAwayTeam = int.Parse(fc["ScoreAwayTeam"]);
            //get HomeTeamId
            var getHomeTeamId = (from t in db.Teams where t.TeamName == match.HomeTeam select t).Take(1).ToList();
            int HomeTeamId = getHomeTeamId[0].TeamId;
            var getRatingId1 = (from r in db.Ratings where (r.TeamId == HomeTeamId && r.TournamentId == match.TournamentId) select r).Take(1).ToList();
            int RatingHomeTeamId = getRatingId1[0].RatingId;
            var ratingHomeTeam = db.Ratings.First(r => r.RatingId == RatingHomeTeamId);
            //get AwayTeamId
            var getAwayTeamId = (from t in db.Teams where t.TeamName == match.AwayTeam select t).Take(1).ToList();
            int AwayTeamId = getAwayTeamId[0].TeamId;
            var getRatingId2 = (from r in db.Ratings where (r.TeamId == AwayTeamId && r.TournamentId == match.TournamentId) select r).Take(1).ToList();
            int RatingAwayTeamId = getRatingId2[0].RatingId;
            var ratingAwayTeam = db.Ratings.First(r => r.RatingId == RatingAwayTeamId);
            if (iScoreHomeTeam > iScoreAwayTeam)
            {
                ratingHomeTeam.Point = ratingHomeTeam.Point + 3;
                ratingHomeTeam.Win = ratingHomeTeam.Win + 1;
                ratingAwayTeam.Lose = ratingAwayTeam.Lose + 1;
            }
            else if (iScoreHomeTeam == iScoreAwayTeam)
            {
                ratingHomeTeam.Point = ratingHomeTeam.Point + 1;
                ratingAwayTeam.Point = ratingAwayTeam.Point + 1;
                ratingHomeTeam.Draw = ratingHomeTeam.Draw + 1;
                ratingAwayTeam.Draw = ratingAwayTeam.Draw + 1;
            }
            else
            {
                ratingAwayTeam.Point = ratingAwayTeam.Point + 3;
                ratingAwayTeam.Win = ratingAwayTeam.Win + 1;
                ratingHomeTeam.Lose = ratingHomeTeam.Lose + 1;
            }
            UpdateModel(ratingHomeTeam);
            UpdateModel(ratingAwayTeam);
            match.ScoreHomeTeam = iScoreHomeTeam;
            match.ScoreAwayTeam = iScoreAwayTeam;
            UpdateModel(match);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        [HttpPost]
        public ActionResult UpdateScorehaha(FormCollection fc,int id = 0)
        {
            var match = db.Matches.First(m => m.MatchId == id);
            int iScoreHomeTeam = int.Parse(fc["ScoreHomeTeam"]);
            int iScoreAwayTeam = int.Parse(fc["ScoreAwayTeam"]);
            //get HomeTeamId
            var getHomeTeamId = (from t in db.Teams where t.TeamName == match.HomeTeam select t).Take(1).ToList();
            int HomeTeamId = getHomeTeamId[0].TeamId;
            var getRatingId1 = (from r in db.Ratings where (r.TeamId == HomeTeamId && r.TournamentId == match.TournamentId) select r).Take(1).ToList();
            int RatingHomeTeamId = getRatingId1[0].RatingId;
            var ratingHomeTeam = db.Ratings.First(r => r.RatingId == RatingHomeTeamId);
            //get AwayTeamId
            var getAwayTeamId = (from t in db.Teams where t.TeamName == match.AwayTeam select t).Take(1).ToList();
            int AwayTeamId = getAwayTeamId[0].TeamId;
            var getRatingId2 = (from r in db.Ratings where (r.TeamId == AwayTeamId && r.TournamentId == match.TournamentId) select r).Take(1).ToList();
            int RatingAwayTeamId = getRatingId2[0].RatingId;
            var ratingAwayTeam = db.Ratings.First(r => r.RatingId == RatingAwayTeamId);
            if (iScoreHomeTeam > iScoreAwayTeam)
            {
                ratingHomeTeam.Point = ratingHomeTeam.Point + 3;
                ratingHomeTeam.Win = ratingHomeTeam.Win + 1;
                ratingAwayTeam.Lose = ratingAwayTeam.Lose + 1;
            }
            else if (iScoreHomeTeam == iScoreAwayTeam)
            {
                ratingHomeTeam.Point = ratingHomeTeam.Point + 1;
                ratingAwayTeam.Point = ratingAwayTeam.Point + 1;
                ratingHomeTeam.Draw = ratingHomeTeam.Draw + 1;
                ratingAwayTeam.Draw = ratingAwayTeam.Draw + 1;
            }
            else
            {
                ratingAwayTeam.Point = ratingAwayTeam.Point + 3;
                ratingAwayTeam.Win = ratingAwayTeam.Win + 1;
                ratingHomeTeam.Lose = ratingHomeTeam.Lose + 1;
            }
            UpdateModel(ratingHomeTeam);
            UpdateModel(ratingAwayTeam);
            match.ScoreHomeTeam = iScoreHomeTeam;
            match.ScoreAwayTeam = iScoreAwayTeam;
            UpdateModel(match);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        
        //
        // POST: /Match/Edit/5

        

        //
        // GET: /Match/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Match match = db.Matches.Find(id);
            if (match == null)
            {
                return HttpNotFound();
            }
            return View(match);
        }

        //
        // POST: /Match/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Match match = db.Matches.Find(id);
            db.Matches.Remove(match);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
        public ActionResult NewIndex()
        {
            var matches = db.Matches.Include(m => m.Stadium).Include(m => m.Tournament).OrderBy(m => m.TournamentId);
            return View(matches.ToList());
        }
    }
}